/*
 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */
package gchisto.gui.utils;

import gchisto.gctrace.GCTrace;
import gchisto.gctrace.GCTraceSet;
import gchisto.gctrace.GCTraceSetListener;
import gchisto.gui.panels.VisualizationPanel;
import gchisto.utils.errorchecking.ArgumentChecking;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;

/**
 *
 * @author  tony
 */
abstract public class TabbedPane<T extends JPanel>
        extends javax.swing.JPanel
        implements VisualizationPanel, GCTraceSetListener {

    final protected Map<GCTrace, T> map = new HashMap<GCTrace, T>();
    
    protected GCTraceSet gcTraceSet;

    abstract protected T newPanel(GCTrace gcTrace);
    
    abstract protected void updatePanel(T panel);
    
    synchronized public void gcTraceAdded(GCTrace gcTrace) {
        T panel = newPanel(gcTrace);
        String name = gcTrace.getName();
        map.put(gcTrace, panel);
        tabbedPane.add(name, panel);
        updatePanel(panel);
    }
    
    synchronized public void gcTraceRenamed(GCTrace gcTrace) {
        T panel = map.get(gcTrace);
        assert panel != null;
        int index = tabbedPane.indexOfComponent(panel);
        String name = gcTrace.getName();
        tabbedPane.setTitleAt(index, name);        
    }
    
    synchronized public void gcTraceRemoved(GCTrace gcTrace) {
        T panel = map.get(gcTrace);
        map.remove(gcTrace);
        tabbedPane.remove(panel);
    }
    
    synchronized public void gcTraceMovedUp(GCTrace gcTrace) {
        T panel = map.get(gcTrace);
        assert panel != null;        assert panel != null;

        String name = gcTrace.getName();
        int index = tabbedPane.indexOfComponent(panel);
        assert index > 0;
        tabbedPane.remove(panel);
        tabbedPane.add(panel, name, index - 1);
    }

    synchronized public void gcTraceMovedDown(GCTrace gcTrace) {
        T panel = map.get(gcTrace);
        assert panel != null;
        String name = gcTrace.getName();
        int index = tabbedPane.indexOfComponent(panel);
        tabbedPane.remove(panel);
        tabbedPane.add(panel, name, index + 1);
    }

    public JPanel getPanel() {
        return this;
    }

    abstract public String getPanelName();

    public GCTraceSetListener getListener() {
        return this;
    }

    public void setGCTraceSet(GCTraceSet gcTraceSet) {
        ArgumentChecking.notNull(gcTraceSet, "gcTraceSet");

        this.gcTraceSet = gcTraceSet;
    }
    
    /** Creates new form TabbedPane */
    public TabbedPane() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables

}
